/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import imageProcessing.classes.ImageHelper;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author damfercer
 */
public class PrimeFacesBufferedImage implements Serializable{
    private BufferedImage bufferedImage;
    private Long identifier;
    
    public PrimeFacesBufferedImage(BufferedImage bi){
        bufferedImage=ImageHelper.deepCopy(bi);
        identifier=UUID.randomUUID().getLeastSignificantBits();
    }

    /**
     * @return the bufferedImage
     */
    public BufferedImage getBufferedImage() {
        return bufferedImage;
    }

    /**
     * @param bufferedImage the bufferedImage to set
     */
    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }
    
    public StreamedContent getStreamedContentBufferedImage() throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();  
        ImageIO.write(bufferedImage, "jpg", os);  
        return new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/jpeg");
    }

    /**
     * @return the identifier
     */
    public Long getIdentifier() {
        return identifier;
    }

    /**
     * @param identifier the identifier to set
     */
    public void setIdentifier(Long identifier) {
        this.identifier = identifier;
    }
}
